# https://leetcode.cn/problems/minimize-xor/description/


class Solution:
    def minimizeXor(self, num1: int, num2: int) -> int:
        b1 = bin(num1)
        b2 = bin(num2)
        c1 = b1.count('1')
        c2 = b2.count('1')
        ans = 0
        if c1 == c2:
            return num1
        if c1 > c2:
            d = c1 - c2
            t = num1
            k = 0
            while t:
                if t & 1 == 1:
                    if d > 0:
                        d -= 1
                    else:
                        ans |= 1 << k
                k += 1
                t >>= 1
        if c1 < c2:
            t = num1
            d = c2 - c1
            k = 0
            while t or d > 0:
                if t & 1 == 1:
                    ans |= 1 << k
                else:
                    if d > 0:
                        ans |= 1 << k
                        d -= 1
                k += 1
                t >>= 1
        return ans
                

so = Solution()
num1 = 65
num2 = 84
print(so.minimizeXor(num1, num2))
